<template>
  <div class="role-container">
    <eForm ref="form" :is-add="isAdd"/>
    <menuDialog ref="menu"/>
    <permissionDialog ref="permission"/>
    <div class="head-container">
      <el-input
        clearable
        placeholder="输入角色名称搜索"
        size="mini"
        style="width: 200px;"
        class="filter-item"
      />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search">搜索</el-button>
      <!-- 新增 -->
      <div style="float:right">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >新增</el-button>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="roles"
      highlight-current-row
      size="small"
      style="width: 100%;"
      border
    >
      <el-table-column prop="role" label="名称"/>
      <el-table-column :show-overflow-tooltip="true" prop="roleName" label="描述"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip content="菜单" placement="top">
            <el-button size="mini" type="primary" class="button-on-margin" icon="el-icon-menu" @click="menu(scope.row)"/>
          </el-tooltip>
          <el-tooltip content="权限" placement="top">
            <el-button size="mini" type="primary" class="button-on-margin" icon="el-icon-s-check" @click="permission(scope.row)"/>
          </el-tooltip>

          <el-button size="mini" type="primary" icon="el-icon-edit" class="button-on-margin" @click="edit(scope.row)"/>
          <el-popover :ref="scope.row.uuid" placement="top" width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.uuid].doClose()">取消</el-button>
              <el-button
                :loading="delLoading"
                type="primary"
                size="mini"
                @click="remove(scope.row.uuid)"
              >确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini" icon="el-icon-delete"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      style="margin-top:20px"
      background
      layout="prev, pager, next"
      :page-count="total"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import eForm from './form';
import menuDialog from './menu';
import permissionDialog from './permission';
import {
  role_page,
  role_remove
} from '@/api/system';
export default {
  components: { eForm, menuDialog, permissionDialog },
  data() {
    return {
      roles: [],
      isAdd: false,
      total: 0,
      delLoading: false
    };
  },
  created() {
    this.getRoles(0);
  },
  methods: {
    async getRoles(page) {
      this.loading = true;
      const res = await role_page({
        page: page,
        pageSize: 10
      });
      this.roles = res.data.pageData;
      this.total = res.data.total;
      if (!this.roles.length) this.showButton = false;
      this.loading = false;
    },
    pageChange(value) {
      console.log(value);
    },
    menu(data) {
      this.$refs.menu.data = data;
      this.$refs.menu.dialog = true;
    },
    permission(data) {
      this.$refs.permission.data = data;
      this.$refs.permission.dialog = true;
    },
    add() {
      this.isAdd = true;
      this.$refs.form.restoreForm();
      this.$refs.form.dialog = true;
    },
    edit(data) {
      this.isAdd = false;
      const _this = this.$refs.form;
      _this.form = {
        uuid: data.uuid,
        role: data.role,
        roleName: data.roleName
      };
      _this.dialog = true;
    },
    async remove(id) {
      try {
        this.delLoading = true;
        await role_remove(id);
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.delLoading = false;
        this.getRoles(0);
      } catch (error) {
        this.delLoading = false;
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.role-span {
  font-weight: bold;
  color: #303133;
  font-size: 15px;
}
.head-container {
  margin-bottom: 20px;
}
</style>
